package com.mycjj.android.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.MyCjjApplication;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.CarInfoBaseBean.CarInfo;
import com.mycjj.android.bean.CarInfoBaseBean;
import com.mycjj.android.bean.UserCarGetsBaseBean;
import com.mycjj.android.bean.UserCarGetsBaseBean.UserCarGetsBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

public class My_CarListActivity extends BaseActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_car_list);
		init();
	}

	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) mHeaderRightButton
				.getLayoutParams();
		params.height = (int) (100 * MyCjjApplication.getInstance()
				.getDisplayDensity());
		params.width = (int) (100 * MyCjjApplication.getInstance()
				.getDisplayDensity());
		mHeaderRightButton.setBackgroundResource(R.color.transparent);
		mHeaderRightButton.setLayoutParams(params);
		mHeaderRightButton.setText("添加车辆");
		mHeaderRightButton.setTextColor(getResources().getColor(R.color.white));
		mHeaderRightButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(My_CarListActivity.this,
						CarInfoActivity.class);
				intent.putExtra("car_state", 2);
				startActivity(intent);
			}
		});
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("车辆列表");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				My_CarListActivity.this.finish();
			}
		});
	}

	private ListView listview;
	private TextView tv_empty;

	private void init() {
		initHeader();
		listview = (ListView) findViewById(R.id.listview);
		tv_empty = (TextView) findViewById(R.id.tv_empty);
		adapter = new CarListAdapter(this);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				CarListAdapter adapter = (CarListAdapter)parent.getAdapter();
//				adapter.setListCheck(position);
				UserCarGetsBean bean = dataList.get(position);
				Intent intent = new Intent(My_CarListActivity.this,
						CarInfoActivity.class);
				intent.putExtra("carId", String.valueOf(bean.getId()));
				intent.putExtra("car_state", 3);
				intent.putExtra("data", bean);
				startActivity(intent);
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		getData();
	}

	List<UserCarGetsBean> dataList;

	private void getData() {
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token",
				Preferences.getSharedPreferences(this, "user_token", ""));
		request.requestUserCarGetsWithDoneHandler("", params,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						UserCarGetsBaseBean baseBean = ProjectDataManage
								.getInstance().getUserCarGetsBaseBean();
						if (baseBean != null) {
							if (baseBean.getCode() == 200) {
								dataList = baseBean.getData();
								if (dataList != null && dataList.size() > 0) {
									listview.setVisibility(View.VISIBLE);
									tv_empty.setVisibility(View.GONE);
									adapter.setList(dataList);
								} else {
									listview.setVisibility(View.GONE);
									tv_empty.setVisibility(View.VISIBLE);
								}
							} else {
								Toast.makeText(
										My_CarListActivity.this,
										baseBean.getMsg(),
										Toast.LENGTH_SHORT).show();
							}
						} else {
							Toast.makeText(My_CarListActivity.this,
									"UserCarGetsBaseBean is empty!!",
									Toast.LENGTH_SHORT).show();
						}
					}
				});
	}

	private CarListAdapter adapter;

	public class CarListAdapter extends TAdapter<UserCarGetsBean> {
		private Map<Integer, Boolean> mpCheckBoolean;
		private List<UserCarGetsBean> mlist;
		public CarListAdapter(Context mContext) {
			super(mContext);
			mpCheckBoolean  =  new HashMap<Integer, Boolean>();
		}
		
		@Override
		public void setList(List<UserCarGetsBean> list) {
			// TODO Auto-generated method stub
			super.setList(list);
			this.mlist = list;//list 数据
			for (int i = 0; i < mlist.size(); i++) {
				mpCheckBoolean.put(i, false);
			}
		}
		public void setListCheck(int positon){
			for (int i = 0; i < mlist.size(); i++) {
				if(i == positon){
					mpCheckBoolean.put(i, true);
				}else{
					mpCheckBoolean.put(i, false);
				}
			}
			notifyDataSetChanged();
		}
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).
						inflate(R.layout.item_my_car_list_view, null);
				holder = new ViewHolder();
				holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				holder.pic = (ImageView) convertView.findViewById(R.id.header_pic);
				holder.cb_check = (CheckBox)convertView.findViewById(R.id.cb_car_check);
				
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			UserCarGetsBean bean = mList.get(position);
			imageLoader.displayImage(bean.getBrand_logo(), holder.pic);
			holder.tv_name.setText(bean.getCar_name());
			holder.cb_check.setOnCheckedChangeListener(null);
			if(mpCheckBoolean.get(position)){
				holder.cb_check.setChecked(true);
			}else{
				holder.cb_check.setChecked(false);
			}
			holder.cb_check.setChecked(isChecked(bean.getId()));
			holder.cb_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if(isChecked){
						setListCheck(position);
						UserCarGetsBean bean = dataList.get(position);
						Preferences.setEditor(mApp, "default_car_id", String.valueOf(bean.getId()));
						getCarInfo(String.valueOf(bean.getId()));
					}
				}
			});
			return convertView;
		}
		
		private boolean isChecked(int id){
			String carId = Preferences.getSharedPreferences(mApp, "carId", "");
			String default_car_id = Preferences.getSharedPreferences(mApp, "default_car_id", carId);
			String currentId  = String.valueOf(id);
			if(currentId.equals(default_car_id)){
				return true;
			}else{
				return false;
			}
		}
		
		private void getCarInfo(String carId){
			request = new ProjectHttpRequestImpl(mContext, true);
			RequestParams params = new RequestParams();
			params.put("car_id", carId);
			params.put("client_id", ProjectHttpRequestInterface.client_id_key);
			params.put("token",
					Preferences.getSharedPreferences(mContext, "user_token", ""));
			request.requestUserCarGetWithDoneHandler("", params,
					new AsyncHttpResponseHandler() {
						@Override
						public void onSuccess(String content) {
							CarInfoBaseBean baseBean = dataManager.getCarInfoBaseBean();
							if (baseBean != null) {
								if (baseBean.getCode() == 200) {
									
									CarInfo info = baseBean.getData();
									Preferences.setEditor(mApp, "default_car_num", info.getNative_plate());
									dataManager.setDefaultCarInfo(info);
									Preferences.setEditor(mApp, "storeId", String.valueOf(info.getStore_id()));
									Preferences.setEditor(mApp, "storeName", info.getStore_name());
//									Preferences.setEditor(mApp, "storeAddress", info.getStore_address());
									Preferences.setEditor(mApp, "storeTel", info.getStore_tel());
									Preferences.setEditor(mApp, "storePoint", info.getStore_point());
									
									Toast.makeText(mContext,
											"设置默认车辆成功!",
											Toast.LENGTH_SHORT).show();
								} else {
									Toast.makeText(mContext,
											"code:" + baseBean.getCode()
													+ ";message:"
													+ baseBean.getMsg(),
											Toast.LENGTH_SHORT).show();
								}
							}
						}
						@Override
						public void onFailure(Throwable error, String content) {
							Toast.makeText(
									mContext,"error msg:"+content,
									Toast.LENGTH_SHORT).show();
						}
					}
					);
		}
		
		

		class ViewHolder {
			TextView tv_name;
			ImageView pic;
			CheckBox cb_check;
		}

	}

}
